﻿using Factory.MasterData.Vendors;
using Factory.WarehouseManagement.WarehouseSettings.DTO;
using Factory.WarehouseManagement.WarehouseSettings;
using Microsoft.AspNetCore.Mvc;

using Factory.WarehouseManagement.Vendors.DTO;
using Factory.WarehouseManagement.Vendors;


namespace Factory.Web.Controllers.WarehouseManagement
{
    public class VendorController : Controller
    {
        private readonly IVendorsAppService _vendorsAppService;

        public VendorController(IVendorsAppService vendorsAppService)
        {
            _vendorsAppService = vendorsAppService;
        }

        public IActionResult VendorIndex()
        {
            return View();
        }

        [HttpGet]
        /// <summary>
        /// 获取数据
        /// </summary>

        /// <returns></returns>
        public JsonResult GetVendorList(string? returnName, int page, int limit)
        {
            var list = _vendorsAppService.GetListAsync(returnName, page, limit);
            return Json(new { data = list.Result, code = 0 });
        }

        public IActionResult VendorAddIndex()
        {
            return View();
        }

        [HttpPost]
        public JsonResult AddVendor(VendorReturn input)
        {
            var arealist = _vendorsAppService.CreateAsync(input);
            return Json(new { code = arealist, msg = "添加成功" });
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <returns>返回对应数据</returns>
        [HttpPost]
        public JsonResult VendorDelete(int id)
        {
            var arealist = _vendorsAppService.DeleteAsync(id);
            return Json(new { code = arealist, msg = "删除成功" });
        }

        public ActionResult VendorUpdateIndex(int id)
        {
            ViewBag.Id = id;
            return View();
        }

        [HttpGet]
        public JsonResult GetVendorById(int id)
        {
            var arealist = _vendorsAppService.GetVendorById(id);
            return Json(new { data = arealist, code = 0 });
        }

        [HttpPost]
        public JsonResult UpdateVendor(VendorDTO input)
        {
            var arealist = _vendorsAppService.UpdateAsync(input);
            return Json(new { code = true, data = arealist });
        }
    }
}
